package com.caigou.service.impl;

import com.caigou.bean.cms.entity.ArticleDictionary;
import com.caigou.bean.vo.FunctionPrivilegeVO;
import com.caigou.mapper.cms.ArticleDictionaryMapper;
import com.caigou.mapper.cms.UserPrivilegeMapper;
import com.caigou.service.ArticleDictionaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("articleDictionaryService")
@Slf4j
public class ArticleDictionaryServiceImpl implements ArticleDictionaryService {

    @Resource
    private ArticleDictionaryMapper articleDictionaryMapper;
    @Resource
    private UserPrivilegeMapper userPrivilegeMapper;
    @Override
    public String loadDictionary(Integer value, Integer type) {
        return articleDictionaryMapper.loadDictionary(value,type);
    }



    @Override
    public List<FunctionPrivilegeVO> findStaffManagePrivilegesByUserId(String userid) {
        List<FunctionPrivilegeVO> keyValueVO = new ArrayList<>();
        List<ArticleDictionary> channelDictionaryList = articleDictionaryMapper.findAllChannelDictionaryList();
        List<Integer> values = channelDictionaryList.stream().map(ArticleDictionary::getValue).collect(Collectors.toList());
        List<ArticleDictionary> articleDictionaries = articleDictionaryMapper.findAllSectionDictionaryByValues(values);
        //将栏目转换为map
        Map<Integer, List<ArticleDictionary>> collect = articleDictionaries.stream().collect(Collectors.groupingBy(ArticleDictionary::getSuperId));
        channelDictionaryList.forEach(channelDictionary -> {
            FunctionPrivilegeVO functionPrivilegeVO = new FunctionPrivilegeVO(channelDictionary.getValue(), channelDictionary.getName());
            List<ArticleDictionary> list1 = collect.get(channelDictionary.getValue());
            if (list1 != null) {
                ArrayList<FunctionPrivilegeVO> objects = new ArrayList<>();
                list1.forEach(lis7t->{
                    FunctionPrivilegeVO keyValueVO3 = new FunctionPrivilegeVO(lis7t.getValue(), lis7t.getName());
                    objects.add(keyValueVO3);
                });
                functionPrivilegeVO.setChildren(objects);
            }
            keyValueVO.add(functionPrivilegeVO);
        });
        return keyValueVO;
    }

    @Override
    public ArticleDictionary loadDictionaryByTitleType(int titleType) {
        return articleDictionaryMapper.loadDictionaryByTitleType(titleType);
    }
}
